﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VTFC
{
    // ----------------------------------------------------------------------------
    /// <summary>
    /// TFS server list form.
    /// </summary>
    // ----------------------------------------------------------------------------
    public partial class FrmServerList : Form
    {       
        // ----------------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        // ----------------------------------------------------------------------------
        public FrmServerList()
        {
            InitializeComponent();
            InitializeContent();

            Initialize();
        }//ctor

        private void InitializeContent()
        {
            this.Text = Resources.FrmServerList_Text;
            this.colServer.Text = Resources.FrmServerList_colServer;
            this.btnAdd.Text = Resources.ButtonAdd;
            this.btnCancel.Text = Resources.ButtonCancel;
            this.btnConnect.Text = Resources.ButtonConnect;
            this.btnDelete.Text = Resources.ButtonDelete;
            this.btnEdit.Text = Resources.ButtonEdit;
        }//func InitializeContent
        
        private void Initialize()
        {
            lvServers.BeginUpdate();

            lvServers.Items.Clear();
            foreach (Server server in ServerManager.ServerContainer.Servers)
            {
                lvServers.Items.Add(new ListViewItemServer()
                {
                    Text = server.Url,
                    Server = server
                });
            }

            lvServers.EndUpdate();

            if (lvServers.Items.Count > 0)
            {
                lvServers.Items[0].Selected = true;
            }
        }//func Initialize

        private void SaveServerList()
        {
            ServerManager.Save();
            Initialize();
        }//func SaveServerList

        private bool ShowServer(Server server)
        {
            bool accept = false;
            using (FrmServerShow frmServerShow = new FrmServerShow())
            {
                frmServerShow.Server = server;
                frmServerShow.Initialize();
                if (frmServerShow.ShowDialog() == DialogResult.OK)
                {
                    accept = true;
                }
            }
            return accept;
        }//func ShowServer

        private ListViewItemServer GetSelectedItem()
        {
            ListViewItemServer item = null;
            if (lvServers.SelectedItems.Count > 0)
            {
                item = lvServers.SelectedItems[0] as ListViewItemServer;
            }
            return item;
        }//func GetSelectedItem

        private Server GetSelectedServer()
        {
            Server server = null;
            ListViewItemServer item = GetSelectedItem();
            if (item != null)
            {
                server = item.Server;
            }
            return server;
        }//func GetSelectedServer

        private void Connect()
        {
            Server server = GetSelectedServer();
            if (server != null)
            {
                ServerManager.ServerContainer.DefaultServerUrl = server.Url;
                SaveServerList();
                this.DialogResult = DialogResult.OK;
            }
        }//func Connect

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Server server = new Server() { UseDefaultCredentials = true };            
            if (ShowServer(server))
            {
                ServerManager.ServerContainer.Servers.Add(server);
                lvServers.Items.Add(new ListViewItemServer()
                {
                    Text = server.Url,
                    Server = server,
                });
            }
            SaveServerList();
        }//func btnAdd_Click

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Server server = GetSelectedServer();
            if (server != null)
            {
                ShowServer(server);
            }
            SaveServerList();
        }//func btnEdit_Click

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ListViewItemServer item = GetSelectedItem();
            if (item != null)
            {
                ServerManager.ServerContainer.Servers.Remove(item.Server);
                
                lvServers.BeginUpdate();
                lvServers.Items.Remove(item);
                lvServers.EndUpdate();
            }
            SaveServerList();
        }//func btnDelete_Click

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Connect();
        }//func btnConnect_Click

        private void lvServers_DoubleClick(object sender, EventArgs e)
        {
            Connect();
        }//func lvServers_DoubleClick

        private void lvServers_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Connect();
            }
        }//func lvServers_KeyDown
    }//class FrmServerList
}//namespace VTFC
